<?php
namespace app\admin\controller;

class Index extends Admin
{   
    private $template;    
    private function getChildren($children){
        $auths = explode(',',$this->auths[0]['rules']);
        foreach ($this->auth_rule as $k => $v){
            if(in_array($v['id'], $children)&&$v['display']&&in_array($v['id'], $auths)){
                $this->template = $this->template.'<li><a _href="/'.$v['name'].'"><i class="iconfont">&#xe6a7;</i><cite>'.$v['title'].'</cite></a></li >';
            }
            
        }
    }
    
    //生成后台管理菜单
    public function index()
    {
        $this->auth_rule = $this->_reSort($this->auth_rule);
        foreach ($this->auth_rule as $k => $v){  
            if($this->auth_rule[$k]['pid']==0&&$this->auth_rule[$k]['display']){
                $this->template =$this->template.'<li><a href="javascript:;"><i class="iconfont">'.$this->auth_rule[$k]['icon'].'</i><cite>'.$this->auth_rule[$k]['title'].
                '</cite><i class="iconfont nav_right">&#xe697;</i></a><ul class="sub-menu">';
                $children = explode(',',$this->auth_rule[$k]['children']);
                $this->getChildren($children);
                $this->template = $this->template.'</ul></li>';
            }
            
        }
        $this->assign("menu_str", $this->template);
        return $this->fetch();
    }

    public function welcome()
    {
        $this->assign("str", '你好');
        return $this->fetch();
    }
    public function _reSort($data, $parent_id='0', $level=0)
    {
        static $_ret = array();
        foreach ($data as $k => $v)
        {
            if($v['pid'] == $parent_id)
            {
                // 标识当前记录是第几层的
                $v['level'] = $level;
                
                $_ret[] = $v;
                // 把这条 记录从数组中删除，再找子类时就不用再循环这条记录了
                unset($data[$k]);
                // 再找当前这条记录的所有的子类
                $this->_reSort($data, $v['id'], $level+1);
            }
        }
        return $_ret;
    }
}
